home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / Writer.java < prev   
Encoding:
Java Source  |  1999-05-28  |  5.1 KB  |  179 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Writer.java    1.16 98/09/21
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * Abstract class for writing to character streams.  The only methods that a
  20.  * subclass must implement are write(char[], int, int), flush(), and close().
  21.  * Most subclasses, however, will override some of the methods defined here in
  22.  * order to provide higher efficiency, additional functionality, or both.
  23.  *
  24.  * @see Writer
  25.  * @see   BufferedWriter
  26.  * @see   CharArrayWriter
  27.  * @see   FilterWriter
  28.  * @see   OutputStreamWriter
  29.  * @see     FileWriter
  30.  * @see   PipedWriter
  31.  * @see   PrintWriter
  32.  * @see   StringWriter
  33.  * @see Reader
  34.  *
  35.  * @version     1.16, 98/09/21
  36.  * @author    Mark Reinhold
  37.  * @since    JDK1.1
  38.  */
  39.  
  40. public abstract class Writer {
  41.  
  42.     /**
  43.      * Temporary buffer used to hold writes of strings and single characters
  44.      */
  45.     private char[] writeBuffer;
  46.  
  47.     /**
  48.      * Size of writeBuffer, must be >= 1
  49.      */
  50.     private final int writeBufferSize = 1024;
  51.  
  52.     /**
  53.      * The object used to synchronize operations on this stream.  For
  54.      * efficiency, a character-stream object may use an object other than
  55.      * itself to protect critical sections.  A subclass should therefore use
  56.      * the object in this field rather than <tt>this</tt> or a synchronized
  57.      * method.
  58.      */
  59.     protected Object lock;
  60.  
  61.     /**
  62.      * Create a new character-stream writer whose critical sections will
  63.      * synchronize on the writer itself.
  64.      */
  65.     protected Writer() {
  66.     this.lock = this;
  67.     }
  68.  
  69.     /**
  70.      * Create a new character-stream writer whose critical sections will
  71.      * synchronize on the given object.
  72.      */
  73.     protected Writer(Object lock) {
  74.     if (lock == null) {
  75.         throw new NullPointerException();
  76.     }
  77.     this.lock = lock;
  78.     }
  79.  
  80.     /**
  81.      * Write a single character.  The character to be written is contained in
  82.      * the 16 low-order bits of the given integer value; the 16 high-order bits
  83.      * are ignored.
  84.      *
  85.      * <p> Subclasses that intend to support efficient single-character output
  86.      * should override this method.
  87.      *
  88.      * @exception  IOException  If an I/O error occurs
  89.      */
  90.     public void write(int c) throws IOException {
  91.     synchronized (lock) {
  92.         if (writeBuffer == null){
  93.         writeBuffer = new char[writeBufferSize];
  94.         }
  95.         writeBuffer[0] = (char) c;
  96.         write(writeBuffer, 0, 1);
  97.     }
  98.     }
  99.  
  100.     /**
  101.      * Write an array of characters.
  102.      *
  103.      * @param  cbuf  Array of characters to be written
  104.      * 
  105.      * @exception  IOException  If an I/O error occurs
  106.      */
  107.     public void write(char cbuf[]) throws IOException {
  108.     write(cbuf, 0, cbuf.length);
  109.     }
  110.  
  111.     /**
  112.      * Write a portion of an array of characters.
  113.      *
  114.      * @param  cbuf  Array of characters
  115.      * @param  off   Offset from which to start writing characters
  116.      * @param  len   Number of characters to write
  117.      *
  118.      * @exception  IOException  If an I/O error occurs
  119.      */
  120.     abstract public void write(char cbuf[], int off, int len) throws IOException;
  121.  
  122.     /**
  123.      * Write a string.
  124.      *
  125.      * @param  str  String to be written
  126.      *
  127.      * @exception  IOException  If an I/O error occurs
  128.      */
  129.     public void write(String str) throws IOException {
  130.     write(str, 0, str.length());
  131.     }
  132.  
  133.     /**
  134.      * Write a portion of a string.
  135.      *
  136.      * @param  str  A String
  137.      * @param  off  Offset from which to start writing characters
  138.      * @param  len  Number of characters to write
  139.      *
  140.      * @exception  IOException  If an I/O error occurs
  141.      */
  142.     public void write(String str, int off, int len) throws IOException {
  143.     synchronized (lock) {
  144.         char cbuf[];
  145.         if (len <= writeBufferSize) {
  146.         if (writeBuffer == null) {
  147.             writeBuffer = new char[writeBufferSize];
  148.         }
  149.         cbuf = writeBuffer;
  150.         } else {    // Don't permanently allocate very large buffers.
  151.         cbuf = new char[len];
  152.         }
  153.         str.getChars(off, (off + len), cbuf, 0);
  154.         write(cbuf, 0, len);
  155.     }
  156.     }
  157.  
  158.     /**
  159.      * Flush the stream.  If the stream has saved any characters from the
  160.      * various write() methods in a buffer, write them immediately to their
  161.      * intended destination.  Then, if that destination is another character or
  162.      * byte stream, flush it.  Thus one flush() invocation will flush all the
  163.      * buffers in a chain of Writers and OutputStreams.
  164.      *
  165.      * @exception  IOException  If an I/O error occurs
  166.      */
  167.     abstract public void flush() throws IOException;
  168.  
  169.     /**
  170.      * Close the stream, flushing it first.  Once a stream has been closed,
  171.      * further write() or flush() invocations will cause an IOException to be
  172.      * thrown.  Closing a previously-closed stream, however, has no effect.
  173.      *
  174.      * @exception  IOException  If an I/O error occurs
  175.      */
  176.     abstract public void close() throws IOException;
  177.  
  178. }
  179.